// 会员系统相关store
import { observable, action } from 'mobx-miniprogram';
import {
  reqGetMyLevelBonusList,
  reqGetMyLevelTaskList,
} from '@/api/membership';

export const membershipStore = observable({
  /** 当前等级索引，0代表1级，依次递增 */
  level: 0,

  /** 上次确认了奖励的等级，0代表1级，依次递增 */
  lastCheckGrade: 0,

  /** 各等级任务 */
  levelTaskList: [] as LevelTaskInfo[],

  /** 各等级礼物比例 */
  levelGiftRatioList: [] as string[],

  /** 奖励列表 */
  levelBonusList: [] as BonusItem[],

  /** 要显示在「升级奖励」页面的奖励列表 */
  showBonusList: [] as BonusItem[],

  /** 获取并设置等级奖励相关信息 */
  getMembershipBonusInfo: action(async function () {
    const res: GetMyLevelBonusListRes = await reqGetMyLevelBonusList();
    this.level = res.data.currentGrade - 1;
    this.lastCheckGrade = res.data.lastCheckGrade - 1;
    this.levelGiftRatioList = Object.keys(res.data.gradeAwardList).map(
      // @ts-ignore
      (key) => res.data.gradeAwardList[key].giftRate,
    );
    this.levelBonusList = Object.keys(res.data.gradeAwardList)
      .map((key) => {
        // @ts-ignore
        const awardList = res.data.gradeAwardList[key]
          .awardList as GradeAwardItem[];
        return awardList.map((item) => ({
          id: item.id,
          icon: item.icon,
          name: item.title,
          validTimeText: item.expireTime,
          remark: item.content,
          levelIndex: Number(key) - 1,
          type: item.type,
          amount: item?.amount,
        }));
      })
      .flat();

    this.showBonusList = res.data.showAwardList.map((item) => ({
      id: item.id.toString(),
      icon: item.icon,
      name: item.title,
      validTimeText: item.expireTime,
      label: item.label,
      remark: item.content,
      levelIndex: item.grade - 1,
      type: item.awardType,
      amount: item?.amount,
      /** 是否已使用 */
      isUsed: item.status === '1',
    })) as BonusItem[];
    console.log('showBonusList', this.showBonusList.slice());
  }),

  /** 获取并设置各等级任务情况 */
  getLevelTaskInfo: action(async function () {
    const res = (await reqGetMyLevelTaskList()) as GetMyLevelTaskInfoRes;
    this.level = res.data.currentGrade - 1;

    this.levelTaskList = res.data.gradeTaskList.map((item) => {
      const taskList =
        item.grade === res.data.currentGrade
          ? res.data.currentTaskList
          : item.taskList;
      return {
        level: item.grade?.toString() || '1',
        isNeedAllCompleted: item.isNeedAllCompleted === '1',
        taskList: taskList.map((task) => {
          const isCompleted =
            item.grade === this.level + 1
              ? task.isComplete === '1'
              : (item?.grade || 1) < this.level + 1
              ? true
              : false;
          const buttonText = isCompleted ? '已完成' : task.buttonText;
          const currentProgress = isCompleted
            ? task.totalProgress
            : task.currentProgress === ''
            ? '0'
            : task.currentProgress;
          const remark = !task.remark
            ? []
            : !task.highLightText
            ? [task.remark]
            : task.remark
                .split(task.highLightText)
                .map((item, index) =>
                  index === 0 ? item : [task.highLightText, item],
                )
                .flat();
          return {
            id: task.id,
            name: task.name,
            type: task.taskType,
            icon: task.icon,
            buttonText,
            isCompleted,
            currentProgress,
            totalProgress: task.totalProgress,
            remark,
          };
        }),
      };
    }) as LevelTaskInfo[];
    console.log('levelTaskList', this.levelTaskList.slice());
  }),
});
